package com.sky.service.impl;

import com.sky.constant.DefaultConstant;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    AddressBookMapper addressBookMapper;
    /**
     * 查询当前登录用户的所有地址信息
     * @return
     */
    @Override
    public List<AddressBook> list() {
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        //查询数据
        List<AddressBook> list = addressBookMapper.list(addressBook);
        return list;
    }

    /**
     * 新增地址
     * @param addressBook
     */
    @Override
    public void addAddressBook(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());

        addressBookMapper.insert(addressBook);
    }

    /**
     * 根据id修改地址
     * @param addressBook
     */
    @Override
    public void updateById(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());

        addressBookMapper.update(addressBook);
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @Override
    public AddressBook getById(Long id) {
        AddressBook addressBook = AddressBook.builder()
                .id(id)
                .build();
        addressBook = addressBookMapper.list(addressBook).get(0);
        return addressBook;
    }

    @Override
    public void deleteById(Long id) {
        addressBookMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void setDefault(Long id) {
        AddressBook addressBook = AddressBook.builder()
                .isDefault(DefaultConstant.IS_DEFAULT)
                .userId(BaseContext.getCurrentId())
                .build();
        //查询是否存在默认地址
        List<AddressBook> list = addressBookMapper.list(addressBook);
        if(list!=null&&list.size()>0){
            //若存在，则设置为非沉默
            AddressBook defaultAdd = list.get(0);
            defaultAdd.setIsDefault(DefaultConstant.IS_NOT_DEFAULT);
            addressBookMapper.update(defaultAdd);
        }
        //设置沉默地址
        addressBook.setId(id);
        addressBookMapper.update(addressBook);
    }

    /**
     * 查询默认地址
     * @return
     */
    @Override
    public AddressBook getDefault() {
        Long userId = BaseContext.getCurrentId();
        AddressBook addressBook = AddressBook.builder()
                .userId(userId)
                .isDefault(DefaultConstant.IS_DEFAULT)
                .build();
        List<AddressBook> list = addressBookMapper.list(addressBook);
        if(list!=null&&list.size()>0){
            return list.get(0);
        }
        return null;
    }


}
